package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.RecruitmentPlan;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.service.IDepartmentService;
import com.rzj.sys_lab_assistant.service.ILabService;
import com.rzj.sys_lab_assistant.service.IRecruitmentPlanService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import com.rzj.sys_lab_assistant.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-01 15:46
 */
@Controller
@RequestMapping("recruitmentPlan")
public class RecruitmentPlanController {

    @Autowired
    IRecruitmentPlanService recruitmentPlanService;
    @Autowired
    ILabService labService;

    private ResultsUtils resultsUtils;

    private TimeUtils timeUtils;

    @RequestMapping("toAdd")
    public String toAdd(Model model, HttpSession session) {
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("lab", labService.listLabsByAccount(technician.getAccount())); // 获取登录的session.technician.account
        return "recruitmentPlan/recruitmentPlanAdd";
    }

    @RequestMapping("toEdit/{planId}")
    public String toEdit(@PathVariable("planId") int planId, Model model) {
        model.addAttribute("plan", recruitmentPlanService.getRecruitmentPlanById(planId));
        return "recruitmentPlan/recruitmentPlanUpdate";
    }

    @RequestMapping("recruitmentPlanListByAccount")
    public String recruitmentPlanListByAccount(Model model, HttpSession session) { // 技术员调用列表
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("planList", recruitmentPlanService.recruitmentPlansList(technician.getAccount())); // 获取登录的session.technician.account
        return "recruitmentPlan/recruitmentPlanByAccountList";
    }

    @RequestMapping("recruitmentPlanList")
    public String recruitmentPlanList(Model model) { // 学工处调用列表
        model.addAttribute("planList", recruitmentPlanService.recruitmentPlanList());
        return "recruitmentPlan/recruitmentPlanList";
    }

    @RequestMapping("recruitmentPlanAdd")
    @ResponseBody
    public Map<String, String> recruitmentPlanAdd(@RequestParam("planName") String planName,
                                                  @RequestParam("requirement") String requirement,
                                                  @RequestParam("planNum") String planNum,
                                                  @RequestParam("applyLab") String applyLab,
                                                  @RequestParam("tId") String tId,
                                                  @RequestParam("startTime") String startTime,
                                                  @RequestParam("endTime") String endTime) throws ParseException {
        RecruitmentPlan recruitmentPlan = new RecruitmentPlan(0, planName, requirement,
                Integer.valueOf(planNum), Integer.valueOf(applyLab), tId,
                timeUtils.getDate(startTime), timeUtils.getDate(endTime));

        int flag = recruitmentPlanService.addRecruitmentPlan(recruitmentPlan);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("recruitmentPlanEdit")
    @ResponseBody
    public Map<String, String> recruitmentPlanEdit(@RequestParam("planId") String planId,
                                                   @RequestParam("planName") String planName,
                                                   @RequestParam("requirement") String requirement,
                                                   @RequestParam("planNum") String planNum,
                                                   @RequestParam("applyLab") String applyLab,
                                                   @RequestParam("tId") String tId,
                                                   @RequestParam("startTime") String startTime,
                                                   @RequestParam("endTime") String endTime) throws ParseException {
        RecruitmentPlan recruitmentPlan = new RecruitmentPlan(Integer.valueOf(planId), planName, requirement,
                Integer.valueOf(planNum), Integer.valueOf(applyLab), tId,
                timeUtils.getDate(startTime), timeUtils.getDate(endTime));

        int flag = recruitmentPlanService.updateRecruitmentPlan(recruitmentPlan);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("recruitmentPlanNumEdit")
    @ResponseBody
    public Map<String, String> recruitmentPlanNumEdit(@RequestParam("planId") String planId,
                                                      @RequestParam("planNum") String planNum) {
        RecruitmentPlan recruitmentPlan = new RecruitmentPlan();
        recruitmentPlan.setPlanId(Integer.valueOf(planId));
        recruitmentPlan.setPlanNum(Integer.valueOf(planNum));

        int flag = recruitmentPlanService.updateRecruitmentPlanNum(recruitmentPlan);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("recruitmentPlanStatusEdit")
    @ResponseBody
    public Map<String, String> recruitmentPlanStatusEdit(@RequestParam("planId") String planId,
                                                         @RequestParam("status") String status) { // 仿照删除方法 发送ajax请求
        RecruitmentPlan recruitmentPlan = new RecruitmentPlan();
        recruitmentPlan.setPlanId(Integer.valueOf(planId));
        recruitmentPlan.setStatus(Integer.valueOf(status));

        int flag = recruitmentPlanService.updateRecruitmentPlanStatus(recruitmentPlan);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("recruitmentPlanDel")
    @ResponseBody
    public Map<String, String> recruitmentPlanDel(@RequestParam("planId") String planId) {

        int flag = recruitmentPlanService.delRecruitmentPlan(Integer.valueOf(planId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }


}
